home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / ka9q / nhclb120.zoo / ax_mbx.c < prev    next >
C/C++ Source or Header  |  1992-06-18  |  48KB  |  1,924 lines

  1. /* Ok, I admit it... what I've done here is a mortal sin, but the XOBBS module
  2.    for ax_mbx.c is so different from the W9NK mailbox, and I hate forcing folks
  3.    to move files around before running a make, so here it is... the two cat'ed
  4.    together with an "if XOBBS ... else ... endif" wrapper... sick, but it
  5.    works...   Bdale */
  6.  
  7.  
  8. static domboxdisplay();
  9. static void free_mbx();
  10. static mbx_line();
  11. static mbx_msg();
  12. static mbx_to();
  13. static mbx_data();
  14. static void mbx_disc();
  15. void fwdxmit();
  16. void fwdrecv();
  17. void fwdstate();
  18. void axbbs_xmit();
  19. char xogetc();
  20.  
  21.  
  22. #ifdef XOBBS
  23. /* XOBBS.C  module to link NET thru IPC message queues to the XOBBS mailbox. */
  24. /* Jim Durham, W2XO, 1-5-89 */
  25. /* requires a call to axchk() to be inserted in the main commutator loop of
  26.     the net main.c code */
  27.  
  28. #include <stdio.h>
  29. #include "global.h"
  30. #include "config.h"
  31. #include "mbuf.h"
  32. #include "ax25.h"
  33. #include "timer.h"
  34. #include "lapb.h"
  35. #include "cmdparse.h"
  36. #include "iface.h"
  37. #include <fcntl.h>
  38. #include <signal.h>
  39. #include <sys/types.h>
  40. #include <sys/ipc.h>
  41. #include <sys/msg.h>
  42. #include "netrom.h"
  43. #include "nr4.h"
  44. #include "ax_mbx.h"
  45. #ifdef    UNIX
  46. #undef    toupper
  47. #undef    tolower
  48. #include <memory.h>
  49. #include <string.h>
  50. #endif
  51.  
  52. #ifdef    BSD
  53. char *sprintf();
  54. #endif
  55.  
  56. /*#define DEBUG       1*/
  57.  
  58. static char conbuf[10][10];        /*buffers used in getcon() and fwdcon()*/
  59. int conindex,conchar;
  60. int fwdflag;            /*flag to do various forwarding tasks*/
  61. int pid;
  62. int fwdbbsstate = NOCON;    /* con/discon state of forwarding bbs process */
  63. int smsgqid;            /* send message queue (from viewpoint of bbs) */
  64. int rmsgqid;            /* receive message queue (from viewpoint of bbs) */
  65. int scmsgqid;            /* send CONTROL message queue */
  66. int rcmsgqid;            /* receive CONTROL message queue */
  67. int lastindex;                  /*last bbs index used*/
  68. int nsend;
  69. int ax25mbox;
  70. char temp[128];
  71. char *bbsexe = "xobbs";        /*path to bbs executable*/
  72. struct  ax25_cb *bbscb;        /*forwarding bbs control block*/
  73. struct message  msg;
  74.  
  75. struct mboxsess * base = NULLMBS;  /*pointer to base of mailbox chain*/
  76. struct mboxsess fwdstruct;    /*forwarding session*/
  77.  
  78. #ifdef SID2
  79. extern struct ax25_addr bbscall;
  80. #endif
  81.  
  82. static struct mboxsess *
  83. newmbox()
  84. {
  85.     struct mboxsess * mbp ;
  86.  
  87.         if(base == NULLMBS){
  88.             base =(struct mboxsess *)malloc(sizeof(struct mboxsess));
  89.             base->next = NULLMBS;
  90.         initmbox(base);
  91.             return base;
  92.         }
  93.         else{
  94.         mbp = base;
  95.         while(mbp->next != NULLMBS)       /*go up the chain to the top*/
  96.             mbp = mbp->next;
  97.         mbp->next =(struct mboxsess *)malloc(sizeof(struct mboxsess));
  98.         mbp->next->next = NULLMBS;
  99.         initmbox(mbp->next);
  100.         return mbp->next;
  101.     }
  102. }
  103.  
  104. initmbox(mbp)
  105.     struct mboxsess * mbp;
  106. {
  107.     
  108.     mbp->bytes = 0;
  109.     mbp->gotbytes = 0;
  110.     mbp->mtxtptr = mbp->rmsgbuf.mtext;
  111. }
  112.  
  113. freembox(mbp)
  114.     struct mboxsess * mbp;
  115. {
  116.     struct mboxsess *p;
  117.     
  118.     if(mbp == base){            /*special case for base session*/
  119.         if(base->next != NULLMBS)/*if base is NOT the only session*/
  120.             base = base->next;   /*then make base point to next session*/
  121.         else
  122.             base = NULLMBS;     /*otherwise,just set base to NULLMBS*/
  123.         free(mbp);              /*free up the storage*/
  124.         return;                 /*and go away*/
  125.     }
  126.     p = base;
  127.     for(;;){
  128.         if(p->next == mbp){     /*if the next upward session is THE one*/
  129.             p->next = mbp->next; /*eliminate the next upward session*/
  130.             free(mbp);
  131.             return;
  132.         }
  133.         if(p->next == NULLMBS){ /*something is wrong here!*/
  134.             free(mbp);          /*try to fix it*/
  135.             return;
  136.         }
  137.         p = p->next;
  138.     }
  139. }
  140.     
  141.             
  142. axchk()        /* service any ax25 connections w2xo 9-1-88*/
  143. {
  144.     int rdcnt;
  145.     struct mbuf *bp;
  146.     struct mboxsess * mbp;
  147.     char *cp;
  148.     int testsize,size;
  149.     void axinit();
  150.     
  151. /* initialization code removed.  call to axinit() direct from main now. */
  152.     
  153.     if(base == NULLMBS)                       /*no mailboxes?..then leave*/
  154.         goto fcontchk;
  155.     mbp = base;                    /*point to the session base*/
  156.     while(mbp != NULLMBS){            /*and climb the session links*/
  157.         if(mbp->gotbytes < 1)            /*if we need input*/
  158.             if((mbp->gotbytes=msgrcv(smsgqid,&mbp->rmsgbuf,1024,(long)mbp->pid,
  159.                IPC_NOWAIT|0600)) < 1)        /*and there is none, ..punt*/
  160.                  goto contchk;
  161.             else                /*got input!*/
  162.                 mbp->mtxtptr = mbp->rmsgbuf.mtext; /*new message buffer, set to start*/
  163.  
  164.             testsize = min(mbp->bytes,mbp->cbadr.axbbscb->paclen+1);      /* get the minimum of the # */
  165.             size = min(testsize, mbp->gotbytes) + 1;       /* of bytes avail and paclen*/
  166.             bp = alloc_mbuf((int16)size);                  /* then min of avail and have*/
  167.             cp = bp->data;
  168.         if(mbp->proto == AX25){
  169.                 *cp++ = PID_FIRST | PID_LAST | PID_NO_L3;
  170.                 bp->cnt =1;
  171.             }
  172.             else  bp->cnt = 0;
  173.             
  174.             while(bp->cnt < size && mbp->gotbytes){
  175.                 *cp++ = *mbp->mtxtptr++;
  176.                 bp->cnt++;
  177.                 mbp->gotbytes--;
  178.             }
  179.  
  180.         if(mbp->proto == AX25)
  181.             send_ax25(mbp->cbadr.axbbscb,bp);
  182.         else
  183.             send_nr4(mbp->cbadr.axnrcb,bp);
  184.                 /*now, check for control messages*/
  185.  
  186. contchk:    rdcnt=msgrcv(scmsgqid,&msg,1024,(long)mbp->pid,IPC_NOWAIT|0600);
  187. #ifdef DEBUG
  188.         if(rdcnt > 0) printf("net:Control Message received: rdcnt=%d\n",rdcnt);
  189. #endif
  190.             switch(rdcnt){
  191.                 case 0:     break;
  192.                 case -1:    /*perror("axchk002");*/
  193.                             break;
  194.                 default:    if(msg.mtext[0] == 'K'){
  195. #ifdef DEBUG
  196.                     printf("recbbs: K received\n");
  197. #endif
  198.                 kill(mbp->pid,9);
  199.                 wait(NULLCHAR);
  200.                 clearque(mbp->pid);
  201.                  if(mbp->proto == AX25)
  202.                      disc_ax25(mbp->cbadr.axbbscb);
  203.                  else
  204.                      disc_nr4(mbp->cbadr.axnrcb);
  205.  
  206.                 freembox(mbp);
  207.                             }/* if K */
  208.             }/* switch */
  209.         mbp = mbp->next;
  210.     }/*while loop*/
  211.  
  212. fcontchk:    chkfwd();            /*check the forwarder*/
  213. }
  214.  
  215. dombox(argc, argv)
  216. int argc ;
  217. char *argv[] ;
  218. {
  219.     if (argc < 2) {
  220.         domboxdisplay() ;
  221.         return 0 ;
  222.     }
  223.  
  224.     if (argv[1][0] == 'y' || (strcmp(argv[1],"on") == 0))
  225.         ax25mbox = 1 ;
  226.     else if (argv[1][0] == 'n' || (strcmp(argv[1],"off") == 0))
  227.         ax25mbox = 0 ;
  228.     else if (argv[1][0] == '?')
  229.         printf("ax25 mailbox is %s\n", ax25mbox ? "on" : "off") ;
  230.     else
  231.         printf("usage: mbox [y|n|?]\n") ;
  232.  
  233.     return 0 ;
  234. }
  235.  
  236. static domboxdisplay()
  237. {
  238.  
  239.     struct mboxsess * mbp;
  240.     printf("Process-ID     &cb      Protocol      User\n") ;
  241.         mbp = base;    
  242.     while(mbp != NULLMBS){
  243.             printf("%-13d%-13x",mbp->pid,mbp->cbadr.axbbscb);             
  244.                 if(mbp->proto == AX25)
  245.                 printf("Ax25        %s\n",mbp->call);
  246.             else
  247.                 printf("Net/Rom     %s\n",mbp->call);
  248.             mbp = mbp->next;
  249.         }/*while loop*/
  250.     printf("\n\n");
  251.     printf("Forwarder State is: ");
  252.     switch(fwdflag){
  253.       case 0: printf("NOFWD\n");
  254.               break;
  255.       case 1: printf("SPAWNBBS\n");
  256.               break;
  257.       case 2: printf("DOCONNECT\n");
  258.               break;
  259.       case 3: printf("READXMIT\n");
  260.               break;
  261.       case 4: printf("GETCON\n");
  262.               break;
  263.       case 5: printf("CLEARCON\n");
  264.               break;
  265.       case 6: printf("WAITING\n");
  266.               break;
  267.       default: printf("UNDEFINED\n");
  268.               break;
  269.     }
  270. }
  271. /* Incoming mailbox session via ax.25 */
  272.  
  273.  
  274. /* * This is the ax25 receive upcall function
  275.  *it gathers incoming data and stuff it down the IPC queue to the proper BBS
  276.  */
  277.  
  278. void
  279. mbx_incom(axp,cnt)    
  280. struct ax25_cb *axp;
  281. int16 cnt;
  282. {
  283. #ifndef SID2
  284.         char arg1[10],arg2[10],arg3[10],arg4[10],arg5[10];    
  285.     void mbx_tx(), mbx_rx(), mbx_state();
  286.     int j;
  287.     struct mboxsess *mbp;
  288.     struct mbuf *bp;
  289.     struct mbuf *recv_ax25();
  290.     
  291.                 mbp =newmbox();        /*after this, this is a mailbox connection*/
  292.                         /* so, make a new mailbox session*/
  293.             axp->r_upcall = mbx_rx ;
  294.             axp->t_upcall = mbx_tx ;
  295.         axp->s_upcall = mbx_state;
  296.                 mbp->cbadr.axbbscb=axp;
  297.